package user.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import db.ComputeSales;
import db.SalesConfig;
import db.UserValidate;

public class ManagerMain extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1166955404671995772L;
	private JPanel contentPane;
	private JTable sales_table;
	private JComboBox month_comboBox;
	private JComboBox year_comboBox;
	private JComboBox date_comboBox;
	private JComboBox year_combo3;
	private JComboBox year_combo2;
	private JComboBox month_combo2;
	private JComboBox week_combo;
	private DefaultTableModel tableModel;
	private String[] cnames={"Flight ID","Sold","Total seats","Price","Max Discount","AirportTax","GST","Net Value","Sales","%utilisation"};
	private JTextArea report_text;
	private Vector<String> columb=new Vector<String>();
	private float totalsale,netvalue;
	private  static ManagerMain instMain;
	/**
	 * Launch the application.
	 */
	
	public static ManagerMain getInstance()
	{
		if(instMain==null)instMain=new ManagerMain();
		return instMain;
	}
	private void changeDays()
	{
		date_comboBox.removeAllItems();
		Calendar c = Calendar.getInstance();
		c.set(year_comboBox.getSelectedIndex() + 2009, month_comboBox.getSelectedIndex(),
				1);

		int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		for (int i = 1; i <= days; i++)
			date_comboBox.addItem(i );

		date_comboBox.setSelectedIndex(0);
	}
	private void initDateCombox()
	{
		for (int i = 2009; i < 2020; i++)
			{year_comboBox.addItem(i);
			year_combo2.addItem(i);
			year_combo3.addItem(i);
			}
		for (int i = 1; i <= 12; i++)
			{month_comboBox.addItem(i);
			month_combo2.addItem(i);
			}
		for (int i = 1; i <= 31; i++)
			date_comboBox.addItem(i);
		for (int i = 1; i <= 365 / 7; i++)
			week_combo.addItem(i);
		Calendar c = Calendar.getInstance();
		System.out.println(c.get(Calendar.YEAR)+"-"+c.get(Calendar.MONTH)+"-"+c.get(Calendar.DATE));
		
		year_comboBox.setSelectedIndex(c.get(Calendar.YEAR)-2009);
		month_comboBox.setSelectedIndex(c.get(Calendar.MONTH));
		
		date_comboBox.setSelectedIndex(c.get(Calendar.DATE)-1);
		
	}
	private String getDateString(Date d, int dayslater) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + dayslater);
		StringBuffer stringBuffer=new StringBuffer();
		stringBuffer.append(now.get(Calendar.YEAR));
		stringBuffer.append("-");
		stringBuffer.append(now.get(Calendar.MONTH)+1);
		stringBuffer.append("-");
		stringBuffer.append(now.get(Calendar.DATE));
		return stringBuffer.toString();
	}
	private String getDateString(int year,int month,int date)
	{
		StringBuffer stringBuffer=new StringBuffer();
		stringBuffer.append(year);
		stringBuffer.append("-");
		stringBuffer.append(month+1);
		stringBuffer.append("-");
		stringBuffer.append(date);
		return stringBuffer.toString();
	}
	private String getDateString() {
		StringBuffer sb = new StringBuffer();
		sb.append((year_comboBox.getSelectedIndex() + 2009));
		sb.append("-");
		sb.append((month_comboBox.getSelectedIndex() + 1));
		sb.append("-");
		sb.append(date_comboBox.getSelectedIndex() + 1);
		return sb.toString();
	}
	private void computeSales(Vector<Vector<String>> r)
	{
		totalsale=0;
		netvalue=0;
		for (Vector<String> vector : r) {
			totalsale+=Float.parseFloat(vector.elementAt(8));
			netvalue+=Float.parseFloat(vector.elementAt(7));
		}
		
	}
	
	@SuppressWarnings("deprecation")
	private void showBy(int type)
	{
		Vector<Vector<String>> r=new Vector<Vector<String>>();
		String beginstr="",endstr="";
		switch (type) {
		case 0:
			
			String today=getDateString();
			beginstr=today;
			endstr=today;
			r=ComputeSales.salesBetween(today, today);
			tableModel.setDataVector(r,columb);
			break;
		case 1:
			Date begin = new Date(year_combo2.getSelectedIndex() + 109, 0, 1);
			int day = week_combo.getSelectedIndex() * 7;
			beginstr= getDateString(begin, day);
			endstr=getDateString(begin, 6+day);
			r=ComputeSales.salesBetween(beginstr, endstr);
			tableModel.setDataVector(r,columb);
			
			
			break;
		case 2:
			beginstr=getDateString(2009+year_combo3.getSelectedIndex(),month_combo2.getSelectedIndex(), 1);
			endstr=getDateString(2009+year_combo3.getSelectedIndex(),month_combo2.getSelectedIndex()+1, 1);
			r=ComputeSales.salesBetween(beginstr, endstr);
			tableModel.setDataVector(r,columb);
		
			
			break;
		default:
			break;
		}
		computeSales(r);
		report_text.setText("table show the sales from '"+beginstr+"' to '"+endstr+"',Total Sales:"+totalsale+",net value:"+netvalue);
	}
	/**
	 * Create the frame.
	 */
	protected ManagerMain() {
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				UserValidate.setExit();
				System.exit(0);
			}
		});
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 833, 559);
		{
			JMenuBar menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			{
				JMenu mnConfig = new JMenu("Manage");
				menuBar.add(mnConfig);
				{
					JMenuItem mntmAirportManage = new JMenuItem("Airport manage");
					mntmAirportManage.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							AirportsFrame.getInstance().setVisible(true);
						}
					});
					mnConfig.add(mntmAirportManage);
				}
				{
					JMenuItem mntmFlightManage = new JMenuItem("Flight Manage");
					mntmFlightManage.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							FlightsFrame.getInstance().setVisible(true);
						}
					});
					mnConfig.add(mntmFlightManage);
				}
				
				JMenuItem mntmGstEdit = new JMenuItem("GST Edit");
				mntmGstEdit.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						String o=SalesConfig.getGST();
						int t=JOptionPane.showConfirmDialog(ManagerMain.getInstance(), "Current GST value is: "+o+" ,Edit?");
						if(t==JOptionPane.OK_OPTION)
						{
							String gst=JOptionPane.showInputDialog(ManagerMain.getInstance(),"please input new GST",o);
							if(gst==null||gst.trim().equals(""))return;
							try{
								float gstv=Float.parseFloat(gst);
								if(SalesConfig.setGST(gstv))
									{
									
									JOptionPane.showMessageDialog(ManagerMain.getInstance(), "update to "+gstv+" successfully");
									return;
									};
							}
							catch (Exception e) {
								// TODO: handle exception
								JOptionPane.showMessageDialog(ManagerMain.getInstance(), "error");
								
							}
							
						}
					}
				});
				mnConfig.add(mntmGstEdit);
			}
			
			JMenu Onlines = new JMenu("Onlines");
			menuBar.add(Onlines);
			
			JMenuItem mntmShowOnlineUsers = new JMenuItem("Show online users");
			mntmShowOnlineUsers.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					OnlinesFrame.getInstance().setVisible(true);
				}
			});
			Onlines.add(mntmShowOnlineUsers);
		}
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(156, 10, 651, 400);
		contentPane.add(scrollPane);
		
		sales_table = new JTable();
		scrollPane.setViewportView(sales_table);
		
		JLabel lblStatistics = new JLabel("View Sales Report");
		lblStatistics.setBounds(10, 10, 141, 28);
		contentPane.add(lblStatistics);
		
		JLabel lblYear = new JLabel("Year");
		lblYear.setBounds(10, 64, 54, 15);
		contentPane.add(lblYear);
		
		year_comboBox = new JComboBox();
		year_comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				changeDays();
			}
		});
		year_comboBox.setBounds(74, 61, 64, 21);
		contentPane.add(year_comboBox);
		
		JLabel lblMonth = new JLabel("Month");
		lblMonth.setBounds(10, 93, 54, 15);
		contentPane.add(lblMonth);
		
		month_comboBox = new JComboBox();
		month_comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				changeDays();
			}
		});
		month_comboBox.setBounds(74, 92, 64, 21);
		contentPane.add(month_comboBox);
		
		JLabel lblDate = new JLabel("Date");
		lblDate.setBounds(10, 122, 54, 15);
		contentPane.add(lblDate);
		
		date_comboBox = new JComboBox();
		date_comboBox.setBounds(74, 123, 64, 21);
		contentPane.add(date_comboBox);
		
		JButton btnShow = new JButton("show");
		btnShow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showBy(0);
			}
		});
		btnShow.setBounds(10, 154, 127, 23);
		contentPane.add(btnShow);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 185, 130, 7);
		contentPane.add(separator);
		
		JLabel lblYear_1 = new JLabel("Year");
		lblYear_1.setBounds(10, 205, 54, 15);
		contentPane.add(lblYear_1);
		
		year_combo2 = new JComboBox();
		year_combo2.setBounds(74, 202, 64, 21);
		contentPane.add(year_combo2);
		
		JLabel lblWeek = new JLabel("Week");
		lblWeek.setBounds(10, 238, 54, 15);
		contentPane.add(lblWeek);
		
		week_combo = new JComboBox();
		week_combo.setBounds(74, 235, 64, 21);
		contentPane.add(week_combo);
		
		JButton btnShow_1 = new JButton("show");
		btnShow_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showBy(1);
			}
		});
		btnShow_1.setBounds(10, 266, 128, 23);
		contentPane.add(btnShow_1);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setBounds(10, 300, 130, 15);
		contentPane.add(separator_1);
		
		JLabel lblYear_2 = new JLabel("Year");
		lblYear_2.setBounds(10, 325, 54, 15);
		contentPane.add(lblYear_2);
		
		year_combo3 = new JComboBox();
		year_combo3.setBounds(74, 325, 64, 21);
		contentPane.add(year_combo3);
		
		JLabel lblMonth_1 = new JLabel("Month");
		lblMonth_1.setBounds(10, 355, 54, 15);
		contentPane.add(lblMonth_1);
		
		month_combo2 = new JComboBox();
		month_combo2.setBounds(74, 356, 64, 21);
		contentPane.add(month_combo2);
		
		JButton btnShow_2 = new JButton("show");
		btnShow_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showBy(2);
			}
		});
		btnShow_2.setBounds(10, 387, 128, 23);
		contentPane.add(btnShow_2);
		tableModel=new DefaultTableModel();
		sales_table.setModel(tableModel);
		sales_table.setRowHeight(25);
		
		report_text = new JTextArea();
		report_text.setEditable(false);
		report_text.setBounds(156, 419, 651, 71);
		contentPane.add(report_text);
		initDateCombox();
		for (String object : cnames) {
			columb.add(object);
		}
		
	}
}
